package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.exception.CustomException;
import com.itheima.mapper.CategoryMapper;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    DishService dishService;
    @Autowired
    SetmealService setmealService;
    @Autowired
    CategoryMapper categoryMapper;



    @Override
    public void deleteById(Long id) {//select count(*) from dish where id = ?id
        //判断dish中有含查询id的菜品
        LambdaQueryWrapper<Dish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Dish::getCategoryId, id);

        int DishCount = dishService.count(lambdaQueryWrapper);
        if (DishCount > 0) {
            throw new CustomException("有菜品,不能删除");
        }
        //判断setmeal中有含查询id的套餐
        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(Setmeal::getCategoryId, id);

        int SetmealCount = setmealService.count(lambdaQueryWrapper2);
        if (SetmealCount > 0) {
            throw new CustomException("有套餐,不能删除");
        }
        //没有直接删除
        categoryMapper.deleteById(id);
    }
}
